var popframe = {
	id: 1,
	zindex: 100020,
	open: function(option){
		var id = this.id;
		var html = $('<div class="popframe"><div class="popframe-header"><a href="javascript:;" title="返回" class="back"> </a><h1></h1></div><div class="popframe-content"></div></div>');
		option.container = option.container || '.container';
		option.title = option.title || '';
		option.skin = option.skin || '';
		option.shade = option.shade === false ? false : true;
		option.shadeClose = option.shadeClose === false ? false : true;
		option.refresh = option.refresh === true ? true : false;
		option.content = option.content || '';
		option.success = option.success || '';
		if(option.shade){
			var share = $('<div id="popframe-share'+ id +'" class="popframe-shade"></div>');
			share.css('z-index', this.zindex++);
			$(option.container).append(share);
			if(option.shadeClose){
				share.click(function(){
					popframe.close(id);
				})
			}
		}
		if(option.skin){
			html.addClass(option.skin);
		}
		if(option.title){
			html.find('.popframe-header h1').html(option.title);
		}
		if(option.refresh){
			html.find('.popframe-header').append('<a href="javascript:;" title="刷新" class="refresh"></a>');
		}
		html.attr('id', 'popframe'+ id);
		html.find('.popframe-content').html(option.content);
		html.css('z-index', this.zindex++);
		$(option.container).append(html);
		if(option.success){
			option.success(id, html);
		}
		html.animate({bottom:0, opacity:1}, 'fast');
		html.find('.popframe-header .back').click(function(){
			popframe.close(id);
		});
		this.id++;

		if(html.find('.popframe-header .back').css("background-image").indexOf("images/close.png")!=-1){
			html.find('.popframe-header .back').css("background","url(static/images/close.png) no-repeat");
		}else{
			html.find('.popframe-header .back').css("background","url(static/images/back.png) no-repeat");
		}
	},
	confirm: function(option){
		var id = this.id;
		var html = $('<div class="popframe-confirm"><div class="popframe-content"></div><div class="popframe-btn"></div></div>');
		option.container = option.container || '.container';
		option.title = option.title || '';
		option.skin = option.skin || '';
		option.shade = option.shade === false ? false : true;
		option.shadeClose = option.shadeClose === false ? false : true;
		option.btn = option.btn || '';
		option.content = option.content || '';
		option.success = option.success || '';
		option.btn0 = option.btn0 || '';
		option.btn1 = option.btn1 || '';
		if(option.shade){
			var share = $('<div id="popframe-share'+ id +'" class="popframe-shade"></div>');
			share.css('z-index', this.zindex++);
			$(option.container).append(share);
			if(option.shadeClose){
				share.click(function(){
					popframe.close(id);
				})
			}
		}
		if(option.skin){
			html.addClass(option.skin);
		}
		if(option.title){
			html.prepend('<div class="popframe-header"><h1>'+ option.title +'</h1></div>');
		}
		html.find('.popframe-content').html(option.content);
		if(option.btn){
			$.each(option.btn, function(i, btn){
				if(i>1){
					return false;
				}
				html.find('.popframe-btn').append('<a href="javascript:;" class="popframe-btn'+ i +'">'+ btn +'</a>');
			});

		}else{
			html.find('.popframe-btn').append('<a href="javascript:;" class="popframe-btn0">取消</a><a href="javascript:;" class="popframe-btn1">确定</a>');
		}
		html.find('.popframe-btn').find('a:last').addClass('pop-btn-default');

		html.attr('id', 'popframe'+ id);
		html.css('z-index', this.zindex++);
		$(option.container).append(html);
		if(option.success){
			option.success(id, html);
		}
		html.find('.popframe-btn0').click(function(){
			if(option.btn0){
				option.btn0(id, html);
			}else{
				popframe.close(id);
			}
		})
		html.find('.popframe-btn1').click(function(){
			if(option.btn1){
				option.btn1(id, html);
			}else{
				popframe.close(id);
			}
		})
		var h = html.height();
		html.animate({top:'50%', marginTop:'-'+(h/2)+'px', opacity:1}, 'fast');
		this.id++;
	},
	close: function(id){
		var frame = $('#popframe'+ id);
		var share = $('#popframe-share'+ id);
		if(frame.length>0){
			var h = frame.height();
			var animate = {bottom:'-'+h+'px', opacity:1};
			if(frame.hasClass('popframe-confirm')){
				animate = {opacity:0};
			}
			frame.animate(animate, 'fast', function(){
				frame.remove();
				if(share.length>0){
					share.remove();
				}
			});
		}
	},
	closeAll: function(){
		$('.popframe').remove();
		$('.popframe-shade').remove();
	}
}